package lijiale.project.employee.service;

import java.sql.SQLException;

import lijiale.project.employee.model.EmployeeModel;
import lijiale.project.util.MD5;

import lijiale.project.employee.dao.EmployeeDao;

public class EmployeeService {

	private EmployeeDao dao=new EmployeeDao();
	
	
	/**  登录功能
	 * @param model
	 * @return  0=账号不存在   1=登陆成功   2=密码错误
	 */
	public String login(EmployeeModel model) {
		EmployeeModel m0=new EmployeeModel(model.getCode());
			EmployeeModel mdb=dao.selectModel(m0);
			if(mdb==null) {
				return "0";//账号不存在
			}
			String pass=MD5.encode(model.getPass()) ;
			return mdb.getPass().equals(pass)?"1":"2";//判断密码是否正确
	}
	
	public String insert(EmployeeModel model) {		
		//业务逻辑:判断账号是否已被注册
		EmployeeModel m0=new EmployeeModel();
		m0.setCode(model.getCode());
		EmployeeModel mdb=dao.selectModel(m0);
		if(mdb==null) {//返回结果为空，执行添加
			//判断密码，若为空，则设置初始密码
			String pass=model.getPass();
			if(pass==null||pass.trim().isEmpty())
			pass="123456";
				//对密码进行加密处理
			model.setPass(MD5.encode(pass));
			//执行插入方法
			Integer res=dao.insert(model);
		return res+"";
		}else {
			return "repeat";
		}
	}
	
	public EmployeeModel selectModel(EmployeeModel model){
		EmployeeModel m=new EmployeeModel(model.getCode());
		EmployeeModel m0=dao.selectModel(m);
		return m0;
	}
	
}